package com.weixing.mall.base.utils;

import java.io.UnsupportedEncodingException;
import java.util.Random;

public class NickNameUtils {

    /**
     * 将字符串转换成相应的字符编码
     *
     * @param s  字符串
     * @return    字符串
     */
    public static String bytes2HexString(String s) {
        byte[] b = s.getBytes();
        s = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            s = s + hex.toUpperCase();
        }
        return s;
    }

    /**
     * 随机生成汉字
     *
     * @return   汉字
     */
    public static String getRandomChar() {
        String str = "";
        int highCode;
        int lowCode;

        Random random = new Random();

        highCode = (176 + Math.abs(random.nextInt(39))); //B0 + 0~39(16~55) 一级汉字所占区
        lowCode = (161 + Math.abs(random.nextInt(93))); //A1 + 0~93 每区有94个汉字

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(highCode)).byteValue();
        b[1] = (Integer.valueOf(lowCode)).byteValue();

        try {
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 随机生成2-4个字的名字
     *
     * @return   名字
     */
    public static String getRandomName() {
        Random random = new Random();
        //名字的个数
        int charNum = random.nextInt(4) % 3 + 4;
        String resultName = "";
        for (int i = 0; i < charNum; i++) {
            String nameWord = getRandomChar();
            resultName += nameWord;
        }
        return resultName;

    }

    public static void main(String[] args) {
        System.out.println(NickNameUtils.getRandomName());
    }
}

